package com.njtoyo.taxi.admin.rest.readfile.driver;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.njtoyo.taxi.admin.library.common.util.CopyUtil;
import com.njtoyo.taxi.admin.logic.business.driver.DriverLogic;
import com.njtoyo.taxi.admin.mapper.master.entity.Driver;
import com.njtoyo.taxi.admin.rest.wrapper.business.driver.DriverBatchRegisterWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.business.driver.DriverRegisterWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class DriverRegisterListener extends AnalysisEventListener<DriverRegisterData> {
    private static final Logger LOGGER = LoggerFactory.getLogger(DriverRegisterListener.class);

    private final DriverRegisterResult result = new DriverRegisterResult();
    private final DriverLogic driverLogic;
    private final DriverBatchRegisterWrapper wrapper;
    private final List<Driver> successResult = new ArrayList<>();
    private final List<DriverRegisterResult.FailData> failResult = new ArrayList<>();

    public DriverRegisterListener(DriverLogic driverLogic, DriverBatchRegisterWrapper wrapper) {
        this.driverLogic = driverLogic;
        this.wrapper = wrapper;
    }

    @Override
    public void invoke(DriverRegisterData item, AnalysisContext context) {
        if (StrUtil.isBlank(item.getName())) {
            return;
        }
        this.result.setUploadCount(this.result.getUploadCount() + 1);
        DriverRegisterWrapper driverWrapper = new DriverRegisterWrapper();
        driverWrapper.setCityId(wrapper.getCityId())
                .setName(item.getName().trim())
                .setMobileNumber(item.getMobileNumber().trim())
                .setPlateNumber(item.getPlateNumber().trim());


        if (ObjectUtil.isNotEmpty(item.getIdentityNumber())) {
            driverWrapper.setIdentityNumber(item.getIdentityNumber().trim());
        }

        if (ObjectUtil.isNotEmpty(item.getServiceNumber())) {
            driverWrapper.setServiceNumber(item.getServiceNumber().trim());
        }

        if (ObjectUtil.isNotEmpty(item.getRoadTransportNumber())) {
            driverWrapper.setRoadTransportNumber(item.getRoadTransportNumber().trim());
        }

        if (ObjectUtil.isNotEmpty(item.getQualificationCertificateNumber())) {
            driverWrapper.setQualificationCertificateNumber(item.getQualificationCertificateNumber().trim());
        }

        try {
            Driver driver = driverLogic.register(driverWrapper);
            this.successResult.add(driver);
            this.result.setSuccessCount(this.result.getSuccessCount() + 1);
        } catch (Exception e) {
            DriverRegisterResult.FailData failData = CopyUtil.copy(item, DriverRegisterResult.FailData.class);
            failData.setMessage(e.getMessage());
            this.failResult.add(failData);
            this.result.setFailCount(this.result.getFailCount() + 1);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        this.result.setSuccessResult(this.successResult);
        this.result.setFailResult(this.failResult);
    }

    public DriverRegisterResult getResult() {
        return result;
    }
}
